/**
 * @description render elem
 */

import { h } from 'snabbdom'
import { DomEditor } from '@wangeditor/editor'
import { getToken } from "@/utils/auth";

function renderAttachment(elem, children, editor) {
    const isDisabled = editor.isDisabled()

    // 当前节点是否选中
    const selected = DomEditor.isNodeSelected(editor, elem)

    // 构建 vnode
    const { fileName = '', link = '' } = elem
    const vnode = h(
        'div',
        {
            props: {
                contentEditable: false, // 不可编辑
            },
            style: {
                display: 'flex',
                alignItems: 'center',
                marginBottom: '12px',
                height: '50px',
                padding: '10px',
                border: '1px solid #DCDFE6',
                borderRadius: '3px', 
                cursor: 'pointer',
            },
            on: {
                click() {
                    if (link) {
                        const token = "Authorization=Bearer " + getToken()
                        window.open(link + '&' + token, '_blank')
                    }
                },
            },
        },
        [
            h('img', {
                props: {
                    src: '',
                },
                style: {
                    width: '1em',
                    height:  '1em',
                    marginRight: '0.1em',
                    minWidth: '0',
                    minHeight: '0',
                },
            }),
            fileName,
        ]
    )

    return vnode
}

const conf = {
    type: 'attachment', // 节点 type 
    renderElem: renderAttachment,
}

export default conf